home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / MyToolSource / MakeAutoMake.cp < prev    next >
Encoding:
Text File  |  1993-12-12  |  3.8 KB  |  190 lines  |  [TEXT/MPS ]

  1. #include <stream.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <Types.h>
  5. #include <CursorCtl.h>
  6.  
  7. const short maxNoFiles = 50;
  8. const short maxNameLen = 40;
  9. char nameTable[maxNoFiles+1][maxNameLen+1];
  10. short noFiles;
  11. short gErr = 0;
  12. Boolean fileHasLF = false;
  13.  
  14. Boolean progress = false;
  15. Boolean verbose = false;
  16. Boolean vverbose = false;
  17.  
  18. char my_spaces[100];
  19. char *SP(int x)
  20. {
  21.     char *p = my_spaces;
  22.     if (x > 40)
  23.     {
  24.         x = 40;
  25.         *p++ = '.';
  26.     }
  27.     for (;x;--x)
  28.         *p++ = ' ';
  29.     *p = 0;
  30.     return my_spaces;
  31. }
  32.  
  33. const char *PP(const char *p)    // skip leading ':' in PartialPaths
  34. {
  35.     if (*p == ':')
  36.         return p + 1;
  37.     else
  38.         return p;
  39. }
  40.  
  41. const char *FN(const char *p)    // find filename in partial/full path
  42. {
  43.     const char *pp = p;
  44.     while (*pp)
  45.     {
  46.         if (*pp++ == ':')
  47.             p = pp;
  48.     }
  49.     return p;
  50. }
  51.  
  52. int partcmp(char* line, char* substr)
  53. {
  54.     while (*substr)
  55.         if (*line++ != *substr++)
  56.             return 1;
  57.     return 0;
  58. }
  59.  
  60. int OpenFile(char *filename, char *partial_path, filebuf &fb)
  61. {
  62.     strcpy(partial_path, filename);
  63.     if (fb.open(filename, input) != 0)
  64.         return 1;
  65.     strcpy(partial_path, ":ES:");
  66.     strcat(partial_path, PP(filename));
  67.     if (fb.open(partial_path, input) != 0)
  68.         return 1;
  69.     return 0;
  70. }
  71.  
  72. void ProcessFile(char *filename, int indent)
  73. {
  74.     RotateCursor(0);
  75.     if (noFiles >= maxNoFiles)
  76.     {
  77.         cerr << "Too many included files: table overflow\n";
  78.         exit(1);
  79.     }
  80.     char partial_path[400];
  81.     filebuf fb;
  82.     if (!OpenFile(filename, partial_path, fb))
  83.     {
  84.         cerr << "Error opening file: " << filename << "\n";
  85.         gErr = 1;
  86.         return;
  87.     }
  88.     short i;
  89.     for (i = 0; i < noFiles; i++)
  90.     {
  91.         if (strcmp(partial_path, nameTable[i]) == 0)
  92.         {
  93.             if (verbose)
  94.                 cerr << form("%3d", indent/2) << SP(indent) << "# " << filename << "\n";
  95.             else if (vverbose)
  96.                 cerr << SP(indent+1) << "Avoided multiple includes: " << partial_path << "\n";
  97.             return;
  98.         }
  99.     }
  100.     if (verbose)
  101.         cerr << form("%3d", indent/2) << SP(indent) << "{ " << partial_path << "\n";
  102.     else if (progress)
  103.         cerr << SP(indent) << "Processing file: " << partial_path << "\n";
  104.     if (indent == 0)
  105.         cout << "\"{ObjApp}" << FN(filename) << ".o\" ƒ";
  106.     cout << " ∂\n\t\t\"{SrcApp}" << PP(partial_path) << "\" ";
  107.     strcpy(nameTable[noFiles++], partial_path);
  108.     fileHasLF = false;
  109.     istream file(&fb);
  110.     while (!file.eof())
  111.     {
  112.         char line[1100];
  113.         file.getline(line, 1000);
  114.         char *p = line;
  115.         while (*p == ' ' || *p == 8) 
  116.             ++p;
  117.         if (!fileHasLF && *p == 10)
  118.         {
  119.             cerr << "Warning: file \"" << partial_path << "\" has linefeeds (LF)\n";
  120.             fileHasLF = true;
  121.             while (*p > 0 && *p == 10) 
  122.                 ++p;
  123.         }
  124.         if (*p != '#')
  125.             continue;
  126.         ++p; // skip '#'
  127.         while (*p == ' ' || *p == 8) 
  128.             ++p;
  129.         if (partcmp(p, "include"))
  130.             continue;
  131.         p += 7;
  132.         while (*p == ' ' || *p == 8) 
  133.             ++p;
  134.         if (*p != '"')
  135.         {
  136.             if (*p != '<')
  137.                 cerr << "Bad #include in file \"" << partial_path << "\" in line \"" << line << "\"\n";
  138.             continue;
  139.         }
  140.         if (vverbose)
  141.             cerr << SP(indent+1) << "Found line containing include: " << line << "\n";
  142.         ++p; // at first letter in filename
  143.         char *endQ = p;
  144.         while (*endQ != '"')
  145.         {
  146.             if (*endQ == 13)
  147.             {
  148.                 cerr << "Missing quote in file: \"" << partial_path << "\" in line \"" << line << "\"\n";
  149.                 gErr = 1;
  150.                 return;
  151.             }
  152.             endQ++;
  153.         }
  154.         short namelen = endQ - p;
  155.         if (namelen > maxNameLen)
  156.         {
  157.             cerr << "Too long filename in file: " << partial_path << "\n";
  158.             gErr = 1;
  159.             return;
  160.         }
  161.         char iname[maxNameLen + 3];
  162.         strncpy(iname, p, namelen);
  163.         iname[namelen] = 0;
  164.         if (vverbose)
  165.             cerr << SP(indent+1) << "Found file filename (" << namelen << "): " << iname << "\n";
  166.         ProcessFile(iname, indent+2);
  167.     }
  168.     if (verbose)
  169.         cerr << "   " << SP(indent) << "} " << partial_path << "\n";
  170. }
  171.  
  172. void DoTheFile(char* filename)
  173. {
  174.     noFiles = 0;
  175.     ProcessFile(filename, 0);
  176.     cout << "\n\n";
  177.     if (verbose)
  178.         cerr << "\n";
  179. }
  180.  
  181. int main(int argc, char *argv[])
  182. {
  183.     InitCursorCtl(nil);
  184.     for (int i = 1; i < argc; i++)
  185.     {
  186.         DoTheFile(argv[i]);
  187.     }
  188.     return gErr;
  189. }
  190.